$(function() {
	layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
		 var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  ,layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,carousel = layui.carousel //轮播
		  ,upload = layui.upload //上传
		  ,element = layui.element //元素操作
		  ,slider = layui.slider //滑块
	var T =	table.render({
	    elem: '#userbatch'
	    ,height: 420
	    ,url: '/selectUserBatch' //数据接口
	    ,title: '学员-批次表'
	    ,page: true //开启分页
	    ,limit:5
	    ,limits:[5,10,15,20]
	    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
	    ,totalRow: true //开启合计行
	    ,response : {
            statusCode : 200// 重新规定成功的状态码为 200，table 组件默认为 0
        }
	    ,parseData : function(res) { // res 即为原始返回的数据
            return {
                "count" : res.tol,
                "code" : res.status, // 解析接口状态
                "msg" : res.message, // 解析提示文本
                "data" : res.list
                // 解析数据列表
            };
        }
	    ,cols: [[ //表头
	      {type: 'checkbox', fixed: 'left'}
	      ,{field: 'user_batch_id', title: 'ID', width:80,align:'center', sort: true, fixed: 'left'}
	      ,{field: 'userId', title: '学员ID',align:'center', width:150}
	      ,{field: 'userName', title: '学员名字',align:'center', width: 200}
	      ,{field: 'batchId', title: '批次ID',align:'center', width:150}
	      ,{field: 'batchName', title: '批次名字',align:'center', width: 200,}
	      ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo5'}
	    ]]
	  });
		 
		 table.on('tool(userbatch)', function(obj){
			    var data = obj.data;
			    
			    if(obj.event === 'del'){
			    	layer.confirm('真的删除行吗',function(index) {
			    		var user_batch_id=data.user_batch_id;
						 $.ajax({
							 url:"/deleteUserBatch",
		             		dataType: 'json',
		                     type: 'post',
		                     data:{user_batch_id:user_batch_id},
		                     success: function () {
		                    	 T.reload();
		                    	 obj.del();
								layer.close(index);
		                     	layer.msg("删除成功");
		                     },
		                     error: function () {
		                        layer.msg("删除失败");
		                     }
						 })
						 })
			    }else if(obj.event=='update'){
			    	layer.open({
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px', '400px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
								+  '  <br>\n'
								
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">用户名字</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text"  id="userName" lay-verify="title" autocomplete="off"  readonly disabled class="layui-input layui-disabled">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								
								+  '  <div class="layui-form-item">\n' 
			                    +  '    <label class="layui-form-label">批次名字</label>\n' 
			                    +  '    <div class="layui-input-block">\n'
			                    +  '    <select id="batchName"></select>\n' 
			                    +  '  </div>\n'
			                    +  '  </div>\n'
								
								+  '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
								+  '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
								+  '</form>'
					});
			    	$("#userName").val(data.userName);
			        
			        $.ajax({
			    		url:"/selectBatchs",
			    		dataType: 'json',
			            type: 'post',
			            success: function (data) {
			            	var da = eval(data);
			            	console.log(da);
			            	$("#batchName").append("<option ></option>");
			            	for(var i=0;i<da.length;i++){
			            		$("#batchName").append("<option value=\""+da[i].batchId+"\">"+da[i].batchName+"</option>");
			            		var form = layui.form;
			            		form.render('select');
			            	}
			            	
			            },
			            error: function () {
			                layer.alert("读取失败");
			            }
			        })
			        
			        $("body").on("click","#btn12",function(){
			        	var batchId=$("#batchName").val();
			        	var user_batch_id=data.user_batch_id;
			        	if(batchId!=null && batchId!=''){
			        	$.ajax({
							url:'/updateUserBatch',
							dataType:'json',
							type:'post',
							data:{
								batchId:batchId,
								user_batch_id:user_batch_id
							},
							success:function(){
									T.reload();
									layer.closeAll();
									layer.msg("修改成功");
							},
							error:function(){
									layer.msg("系统错误11");
								
							}
						})
					}else{
						layer.msg("存在空值，请填写完整再提交")
					}
				
				
			        	
			        })
			    }
			  });
		 $("#add5").on("click",function(){
			 layer.open({
					type : 1,
					skin : 'layui-layer-rim', // 加上边框
					area : [ '520px', '400px' ], // 宽高
					content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
							+ '  <br>\n'
							+ '  <div class="layui-form-item">\n'
							+ '    <label class="layui-form-label">用户批次ID</label>\n'
							+ '    <div class="layui-input-block">\n'
							+ '      <input type="text" id="userbatchid"  class="layui-input">\n'
							+ '    </div>\n'
							+ '  </div>\n'
							+  '  <div class="layui-form-item">\n' 
		                    +  '    <label class="layui-form-label">学生名字</label>\n' 
		                    +  '    <div class="layui-input-block">\n'
		                    +  '    <select id="userName"></select>\n' 
		                    +  '  </div>\n'
		                    +  '  </div>\n'
							+  '  <div class="layui-form-item">\n' 
		                    +  '    <label class="layui-form-label">批次名字</label>\n' 
		                    +  '    <div class="layui-input-block">\n'
		                    +  '    <select id="batchName"></select>\n' 
		                    +  '  </div>\n'
		                    +  '  </div>\n'
							
							+ '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
							+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
							+ '</form>'
				});
			 $.ajax({
		    		url:"/selectBatchs",
		    		dataType: 'json',
		            type: 'post',
		            success: function (data) {
		            	var da = eval(data);
		            	console.log(da);
		            	$("#batchName").append("<option ></option>");
		            	for(var i=0;i<da.length;i++){
		            		$("#batchName").append("<option value=\""+da[i].batchId+"\">"+da[i].batchName+"</option>");
		            		var form = layui.form;
		            		form.render('select');
		            	}
		            	
		            },
		            error: function () {
		                layer.alert("读取失败");
		            }
		        })
		        
		         $.ajax({
		    		url:"/selectUseres",
		    		dataType: 'json',
		            type: 'post',
		            success: function (data) {
		            	var da = eval(data);
		            	console.log(da);
		            	$("#userName").append("<option ></option>");
		            	for(var i=0;i<da.length;i++){
		            		$("#userName").append("<option value=\""+da[i].userId+"\">"+da[i].userName+"</option>");
		            		var form = layui.form;
		            		form.render('select');
		            	}
		            	
		            },
		            error: function () {
		                layer.alert("读取失败");
		            }
		        })
		       $("body").on("click","#btn12",function(){
		    	   var userId=$("#userName").val();
		    	   var batchId=$("#batchName").val();
		    	   var user_batch_id=$("#userbatchid").val();
		    	   console.log(userId);
		    	   console.log(batchId);
		    	   console.log(user_batch_id);
		    		if(userId!=null && batchId!=null && userId!='' && batchId!='' && user_batch_id!=null && user_batch_id!=''){
						$.ajax({
							url:'/insertUserBatch',
							dataType:'json',
							type:'post',
							data:{
								userId:userId,
								batchId:batchId	,
								user_batch_id:user_batch_id
							},
							success:function(res){
								if(res.message=="yes"){
									T.reload();
									layer.closeAll();
									layer.msg("插入成功");
								}else{
									layer.msg("ID相同或该学员已经被分配了！");
								}
									
						
							},
							error:function(){
									layer.msg("系统错误11");
								
							}
						})
					}else{
						layer.msg("存在空值，请填写完整再提交")
					}
		       }) 
		        
		 })
		 
	})
})